package com.example.anydo.util.pub_util;

import android.app.Activity;
import android.util.Log;
import android.widget.LinearLayout;

import com.example.anydo.bean.CommonBean;
import com.example.anydo.bean.OperationBean;
import com.example.anydo.bean.SubTaskBean;
import com.example.anydo.bean.SynchronizeBodyBean;
import com.example.anydo.bean.TaskBean;
import com.example.anydo.bean.UserBean;
import com.example.anydo.constant.Url;
import com.example.anydo.net.DataBack;
import com.example.anydo.net.NetClient;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.database.table_info.OperationTable;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class Synchronizer {

    private final Activity activity;

    private boolean state = false;
    private DatabaseHelper helper;

    public Synchronizer(Activity activity){
        this.activity = activity;
    }

    public void downloadData(String id, DataBack back){
        NetClient.getInstance()
                .withUrl(Url.BASE_URL + Url.GET_DATA_URL)
                .activity(activity)
                .setPlayer("同步数据中")
                .addParams("id",id)
                .asObject(SynchronizeBodyBean.class)
                .doGet(back);
    }


    private SynchronizeBodyBean.DataBean getCacheData(Integer id){

        SynchronizeBodyBean.DataBean data = new SynchronizeBodyBean.DataBean();
        helper = new DatabaseHelper(activity);
        List<OperationBean> operations = helper.readAllOperation(id);

        // 设置 User
        data.setUser(helper.getUserData(id).getData());

        ArrayList<TaskBean.DataBean> tasks = new ArrayList<>();
        ArrayList<SubTaskBean.DataBean> subTasks = new ArrayList<>();
        ArrayList<String> taskCrud = new ArrayList<>();
        ArrayList<String> subTaskCrud = new ArrayList<>();

        for (OperationBean op: operations) {
            switch (op.getType()){
                case OperationTable.TASK_TYPE:
                    tasks.add(helper.getTaskById(op.getTypeId()));
                    taskCrud.add(op.getCrud());
                    break;
                case OperationTable.SUB_TASK_TYPE:
                    if(op.getCrud().equals(OperationTable.CRUD_D)){
                        SubTaskBean.DataBean temp = new SubTaskBean.DataBean();
                        temp.setId(-1);
                        temp.setTitle("delete");
                        temp.setTaskId(op.getTypeId());
                        subTasks.add(temp);
                        subTaskCrud.add(OperationTable.CRUD_D);
                    }else {
                        subTasks.add(helper.getSubTaskById(op.getTypeId()));
                        subTaskCrud.add(op.getCrud());
                    }
                    break;
            }
        }
        data.setTasks(tasks);
        data.setSubTasks(subTasks);
        data.setTasksCrud(taskCrud);
        data.setSubTaskCrud(subTaskCrud);

        Log.e("SYN", "getCacheData: " + data.toString() );
        return data;
    }
    public void postData(Integer id){

        Gson gson = new Gson();
        String json = gson.toJson(getCacheData(id));
        Log.e("SYN", "postJsonData: "+json );

        NetClient.getInstance()
                .withUrl(Url.BASE_URL + Url.POST_DATA_URL)
                .activity(activity)
                .setPlayer("同步数据中")
                .addParams("id",id+"")
                .asObject(CommonBean.class)
                .doJsonPost(json, new DataBack<CommonBean>() {
                    @Override
                    public void onResponse(CommonBean data) {
                        if(data.getCode() == 200){
                            Util.toast(activity,data.getMessage());
                            helper.deleteAllOperations(Integer.parseInt(SPUtil.getUserId(activity)));
                        }else {
                            Util.toast(activity,data.getMessage());
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        Util.toast(activity,"出了一点点问题...");
                    }
                });
    }
}
